package com.example.automanagement.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.example.automanagement.dto.UserDto;
import com.example.automanagement.entity.*;
import com.example.automanagement.mapper.UserMapper;
import com.example.automanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User userLogin(String userId, String userPassword) {
        return userMapper.userLogin(userId,userPassword);
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void addUserRole(List<UserRole> userRoleList) {
        userMapper.addUserRole(userRoleList);
    }

    @Override
    public void userUpdateInformation(User user) {
        userMapper.userUpdateInformation(user);
    }

    @Override
    public void userUpdatePassword(String userId, String oldPassword, String newPassword) {
        userMapper.userUpdatePassword(userId,oldPassword,newPassword);
    }

    @Override
    public void adminUpdateUser(User user) {
        userMapper.adminUpdateUser(user);
    }

    @Override
    public void deleteUser(String userId) {
        userMapper.deleteUser(userId);
    }

    @Override
    public void deleteUserRole(String userId) {
        userMapper.deleteUserRole(userId);
    }

    @Override
    public List<UserRole> getUserRole(String userId) {
        return userMapper.getUserRole(userId);
    }

    @Override
    public List<RoleBusinessMenu> getRoleBusiness(String userRole) {
        return userMapper.getRoleBusiness(userRole);
    }

    @Override
    public List<Role> getAllRoles() {
        return userMapper.getAllRoles();
    }

    @Override
    public List<RoleBusinessMenu> getOtherMenus(String userRole) {
        return userMapper.getOtherMenus(userRole);
    }

    @Override
    public void addRoleMenu(List<RoleBusiness> roleBusinessMenuList) {
        userMapper.addRoleMenu(roleBusinessMenuList);
    }

    @Override
    public void addRole(String roleName) {
        userMapper.addRole(roleName);
    }

    @Override
    public void deleteRole(String roleName) {
        userMapper.deleteRole(roleName);
    }

    @Override
    public void deleteRoleMenu(String userRole, int businessId) {
        userMapper.deleteRoleMenu(userRole,businessId);
    }

    @Override
    public List<UserDto> getAllUser(){
        List<User> allUser = userMapper.getAllUser();
        List<UserDto> userDtos = BeanUtil.copyToList(allUser, UserDto.class);

        return userDtos;
    };

    @Override
    public List<String> getAllDepartment(){
        List<User> temp=userMapper.getAllUser();
        List<String> department = new ArrayList<>();
        for (User user : temp) {
            String depart = user.getUserDepartment();
            if (!department.contains(depart)) {
                department.add(depart);
            }
        }
        return department;
    };

    @Override
    public List<String> getAllPosition(){
        List<User> temp=userMapper.getAllUser();
        List<String> position = new ArrayList<>();
        for (User user : temp) {
            String depart = user.getUserPosition();
            if (!position.contains(depart)) {
                position.add(depart);
            }
        }
        return position;
    };
    @Override
    public List<User> getUserInfoById(String userId) {
        return userMapper.getUserInfoById(userId);
    }

    @Override
    public User getUserById(String userId) {
        return userMapper.getUserById(userId);
    }
}
